package com.ae.base.client.event;

import com.ae.base.ModConstant;
import com.ae.base.net.KeyDown;
import com.ae.base.net.ModNetWork;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(modid = ModConstant.MOD_ID,bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class KeyInput {
    //默认快捷键L
    public static final KeyBinding PUT_KEY = new KeyBinding("key.put", KeyConflictContext.IN_GAME, KeyModifier.NONE, InputMappings.Type.KEYSYM, GLFW.GLFW_KEY_K, "key." + ModConstant.MOD_ID);

    //该快捷键用于放置物品
    @SubscribeEvent
    public static void putKey(InputEvent.KeyInputEvent event) {
        if (PUT_KEY.isDown()) {
            ClientPlayerEntity player = Minecraft.getInstance().player;
            if (player != null) {
                BlockRayTraceResult result = (BlockRayTraceResult) player.pick(4d, 0f, false);
                if (result.getType() == RayTraceResult.Type.BLOCK) {
                    ModNetWork.CHANNEL.sendToServer(new KeyDown(new BlockPos(result.getBlockPos()), result.getDirection()));
                }
            }
        }
    }
}
